﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.Reflection;
using System.Web.SessionState;

namespace jmwqz.ajax
{
    /// <summary>
    /// user 的摘要说明
    /// </summary>
    public class user : IHttpHandler,IRequiresSessionState
    {

        private Maticsoft.BLL.tb_userdata buser = new Maticsoft.BLL.tb_userdata();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                int userid = 0;
                int.TryParse(context.Session["userid"].ToString(), out userid);

                int itemid = 0;
                int.TryParse(context.Session["itemid"].ToString(), out itemid);

                List<Maticsoft.Model.tb_userdata> users = buser.GetModelList(" ");
                users.Sort(new UserDataCompare());
                List<userdataEx> datas = new List<userdataEx>();
                for (int i = 0; i < users.Count && i < 100; i++)
                {
                    userdataEx ex = new userdataEx();
                    ex.rank = i + 1;
                    ex.jifen = users[i].jifen + users[i].freezejifen;
                    ex.nickname = users[i].realname;
                    ex.tj_num = users[i].tj_num;
                    ex.headimg = users[i].imgurl;
                    datas.Add(ex);
                }
                context.Response.Write(JsonConvert.SerializeObject(datas));
            }
            catch
            {
                context.Response.Write("1");
            }           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public class userdataEx
        {
            public int rank { get; set; }
            public decimal jifen { get; set; }
            public string nickname { get; set; }
            public string headimg { get; set; }
            public int tj_num { get; set; }
        }
    }

    public class UserDataCompare : IComparer<Maticsoft.Model.tb_userdata>
    {
        public int Compare(Maticsoft.Model.tb_userdata a, Maticsoft.Model.tb_userdata b)
        {
            if (a.jifen + a.freezejifen > b.jifen + b.freezejifen)
            {
                return -1;
            }
            else
            {
                return 1;
            }
            return a.jifen.CompareTo(b.jifen);
        }
    }
}